// Calculation_Abilities.cpp: Implementierungsdatei
//

#include "stdafx.h"
#include "Charplan.h"
#include "Calculation_Abilities.h"
#include "Charplandoc.h"


// CCalculation_Abilities-Dialogfeld

IMPLEMENT_DYNCREATE(CCalculation_Abilities, CCalculationPage)

CCalculation_Abilities::CCalculation_Abilities(CWnd* pParent /*=NULL*/)
        : CCalculationPage(pParent)
{
    mn_last_viewed_description = -1;
}

// for runtime creations
BOOL CCalculation_Abilities::Create(CWnd* pParentWnd)
{
    return CDialog::Create(CCalculation_Abilities::IDD,pParentWnd);
}

CCalculation_Abilities::~CCalculation_Abilities()
{
}

void CCalculation_Abilities::DoDataExchange(CDataExchange* pDX)
{
    CCalculationPage::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_LIST1, mwnd_armor);
    DDX_Control(pDX, IDC_LIST2, mwnd_abilities);
    DDX_Control(pDX, IDC_RESISTENCES, mwnd_resistenzes);
}


BEGIN_MESSAGE_MAP(CCalculation_Abilities, CCalculationPage)
    ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST1, &CCalculation_Abilities::OnNMCustomdrawList1)
    ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST2, &CCalculation_Abilities::OnNMCustomdrawList1)
END_MESSAGE_MAP()


BOOL CCalculation_Abilities::OnInitDialog()
{
    CCalculationPage::OnInitDialog();

    mwnd_armor.InsertColumn(0,_T("Level"));
    mwnd_armor.InsertColumn(1,_T("Train"));
    mwnd_armor.InsertColumn(2,_T("Name"));
    mwnd_armor.SetExtendedStyle(LVS_EX_FULLROWSELECT);

    mwnd_abilities.InsertColumn(0,_T("Level"));
    mwnd_abilities.InsertColumn(1,_T("Train"));
    mwnd_abilities.InsertColumn(2,_T("Name"));
    mwnd_abilities.SetExtendedStyle(LVS_EX_FULLROWSELECT);

    mwnd_resistenzes.InsertColumn(0,_T("Resi"));
    mwnd_resistenzes.InsertColumn(1,_T("Value"));
    mwnd_resistenzes.SetExtendedStyle(LVS_EX_FULLROWSELECT);

    return TRUE;
}


void CCalculation_Abilities::OnRaceChanged()
{

    CStringArray names;
    IntArray  percents;
    gdbinterface.GetRaceResis(names,percents);


    mwnd_resistenzes.DeleteAllItems();

    for (int t=0;t<names.GetCount();t++)
    {
        int idx = mwnd_resistenzes.InsertItem(t,names[t]);

        CString temp;
        temp.Format("%i %%",percents[t]);

        mwnd_resistenzes.SetItemText(idx,1,temp);
    }

    mwnd_resistenzes.SetColumnWidth(0,LVSCW_AUTOSIZE);
    mwnd_resistenzes.SetColumnWidth(1,LVSCW_AUTOSIZE);
}






void CCalculation_Abilities::OnClassChanged()
{
    CStringArray names;
    IntArray  levels;
    IntArray  type;

    gdbinterface.GetListOfAbilities(names,levels,type);

    mwnd_armor.DeleteAllItems();
    mwnd_abilities.DeleteAllItems();

    int  mn_evade = 0;
    bool mb_shield_carry = false;
    bool mb_hitpointsgain = false;
    bool mb_parry_able = false;
    bool mb_parry_skill = false;
    bool mb_shield_skill = false;


    for (int t=0;t<names.GetCount();t++)
    {
        CString str_level;
        str_level.Format(_T("%i"),levels[t]);
        if (levels[t]>80) str_level.Empty();

        // check if normal or enhanced
        CListCtrlMoreNotifies* pc_list = &mwnd_abilities; // right window
        CString temp = names[t];
        temp.MakeLower();

        // special
        if (type[t]>0 && (type[t]&15)!=7)
        {
            pc_list = &mwnd_armor; // Left window

            // check 'Shield'
            if (temp.Left(6)=="shield")  mb_shield_carry = true;
            else
                if (temp.Left(6)=="schild")  mb_shield_carry = true;

            if (temp.Left(5)=="parie")  mb_parry_able = true;
            else
                if (temp.Left(5)=="parry")  mb_parry_able = true;
        }
        else
        {
            // count "evade"
            if (temp.Left(8)=="ausweich") mn_evade++;
            else
            if (temp.Left(5)=="evade")  mn_evade++;

            if (temp=="kampfnarben")  mb_hitpointsgain = true;
            else
            if (temp.Left(9)=="hitpoints") mb_hitpointsgain = true;

            if (temp.Left(5)=="parie")  mb_parry_able = true;
            else
            if (temp.Left(5)=="parry")  mb_parry_able = true;
        }



        int item = pc_list->InsertItem(LVIF_TEXT|LVIF_PARAM,
                                       pc_list->GetItemCount(),str_level,
                                       0,0,0,type[t]&15);

        if ((type[t]&15)==1) pc_list->SetItem(item,1,LVIF_TEXT,_T("x"),0,0,0,NULL);
        pc_list->SetItem(item,2,LVIF_TEXT,names[t],0,0,0,NULL);
    }

    // set columns width
    mwnd_abilities.SetColumnWidth(0,LVSCW_AUTOSIZE);
    mwnd_abilities.SetColumnWidth(1,LVSCW_AUTOSIZE);
    mwnd_abilities.SetColumnWidth(2,LVSCW_AUTOSIZE);

    mwnd_armor.SetColumnWidth(0,LVSCW_AUTOSIZE);
    mwnd_armor.SetColumnWidth(1,LVSCW_AUTOSIZE);
    mwnd_armor.SetColumnWidth(2,LVSCW_AUTOSIZE);


    // M.Jurkait [5/1/2006]
    // -------------------------
    // Check skill-ables
    // -------------------------
    gdbinterface.GetListOfStyleLines(names);

    for (int t=0;t<names.GetCount();t++)
    {
        if (names[t].Left(6).MakeLower()=="shield")  mb_shield_skill = true;
        else
            if (names[t].Left(6).MakeLower()=="schild")  mb_shield_skill = true;

        if (names[t].Left(5).MakeLower()=="parie")  mb_parry_skill = true;
        else
            if (names[t].Left(5).MakeLower()=="parry")  mb_parry_skill = true;
    }



    GetPage()->mn_evade = mn_evade;
    GetPage()->mb_shield_carry = mb_shield_carry;
    GetPage()->mb_hitpointsgain = mb_hitpointsgain;
    GetPage()->mb_parry_able = mb_parry_able;
    GetPage()->mb_parry_skill = mb_parry_skill;
    GetPage()->mb_shield_skill = mb_shield_skill;

}






void CCalculation_Abilities::OnNMCustomdrawList1(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMLVCUSTOMDRAW  lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;

    *pResult = CDRF_DODEFAULT;
    switch (lplvcd->nmcd.dwDrawStage)
    {
        case CDDS_PREPAINT:
            *pResult = CDRF_NOTIFYITEMDRAW;          // ask for item notifications.
            break;

        case CDDS_ITEMPREPAINT:
            *pResult = CDRF_DODEFAULT;

            switch (lplvcd->nmcd.lItemlParam)
            {
                case 1: // skills
                case 2:
                    lplvcd->clrTextBk = RGB(232, 232, 255);
                    break;
                case 3:
                    lplvcd->clrTextBk = RGB(240, 240, 240);
                    lplvcd->clrText = RGB(128, 0, 0);
                    break;
                case 4:
                    lplvcd->clrTextBk = RGB(240, 240, 240);
                    lplvcd->clrText = RGB(0, 92, 0);
                    break;
                case 5:
                    lplvcd->clrTextBk = RGB(255, 196, 196);
                    lplvcd->clrText = RGB(0, 0, 0);
                    break;

            }
            break;
    }
}



// DESCRIPTION
BOOL CCalculation_Abilities::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
    if (message != LVN_MOUSEMOVE)
        return CCalculationPage::OnWndMsg(message, wParam, lParam, pResult);

    int item = LOWORD(wParam);
    bool b_left =false;
    CListCtrlMoreNotifies* pctrl = &mwnd_abilities;
    if (HIWORD(wParam)==IDC_LIST1)
    {
        b_left = true;
        pctrl = &mwnd_armor;
    }


    if (item != -1 && (mn_last_viewed_description != item+1 || mb_last_viewed_description_was_left != b_left))
    {
        mn_last_viewed_description = item+1;
        mb_last_viewed_description_was_left = b_left;

        CString text;
        text = pctrl->GetItemText(item,2);

        mToolTips.UpdateTipText(gdbinterface.GetAbilityDesc(text),pctrl);
    }

    return TRUE;
}
